package com.itshidu.jiaowu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itshidu.jiaowu.dao.ClassesMapper;
import com.itshidu.jiaowu.entity.Classes;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.service.ClassesService;
import com.itshidu.jiaowu.service.StudentService;
import com.itshidu.jiaowu.service.UserService;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.StudentVo;
import com.itshidu.web.tools.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */
@Controller
public class StudentAction {
    @Autowired StudentService studentservice;
    @Autowired UserService userservice;
    @Autowired ClassesService classesService;
    @Autowired ClassesMapper classesMapper;


    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }


    //查询学生个人信息
    @ResponseBody
    @RequestMapping("/admin/student")
    public Object student(HttpSession session, StudentVo studentvo, ModelAndView v){
        StudentVo  s = studentservice.student(studentvo);
        v.addObject("sss",s );
        v.setViewName("student");
        return  v;
    }

    //添加学生
    @ResponseBody
    @PostMapping("/admin/student/save")
    public Result StudentSave(User user,String college_name,String graduate_date,String join_date,String parent_info,String idc_code,String classname) {
      Result r=studentservice.save(user,college_name,graduate_date,join_date,parent_info,idc_code,classname);
        return r;
    }

    //学生信息添加板块里的班级框的点击查询
    @ResponseBody
    @RequestMapping("/calssname")
    public Object major(String keyword) {

        QueryWrapper<Classes> studentwrapper = new QueryWrapper<>();
        studentwrapper.like("name", keyword);
        List list= classesMapper.selectList(studentwrapper);
        return R.of().put("value",list);
    }


    //更新个人信息
    @ResponseBody
    @PostMapping("/admin/student/update")
    public Result StudentUpdate(User user,String college_name,String graduate_date,String join_date,String parent_info,String idc_code,String site) {
        Result r=studentservice.xiugai(user,college_name,graduate_date,join_date,parent_info,idc_code,site);
        return r;
    }

    //修改密码
    @ResponseBody
    @RequestMapping("/admin/student/password")
    public Result updatepwd(String oldpwd,String newpwd1,String newpwd2){
        Result result=userservice.updatepwd(oldpwd,newpwd1,newpwd2);
        System.out.println(result);
        return result;
    }
    //查询所有学生
    @ResponseBody
    @RequestMapping("/admin/AllStudent")
    public Result selectAll(Integer page,Integer limit){
        return studentservice.selectall(page,limit);
    }
}

